//
// Created by 86155 on 2025/4/16.
//

#ifndef LLVM_FEASMPRINTER_H
#define LLVM_FEASMPRINTER_H
#include "llvm/CodeGen/AsmPrinter.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/MC/MCStreamer.h"
#include "FeTargetMachine.h"

namespace llvm {
class FeSubtarget;
class FeAsmPrinter : public AsmPrinter{
public:
  const FeSubtarget *Subtarget;
  explicit FeAsmPrinter(TargetMachine &TM,
                         std::unique_ptr<MCStreamer> Streamer)
      : AsmPrinter(TM, std::move(Streamer)) {
    Subtarget = static_cast<FeTargetMachine &>(TM).getSubtargetImpl();
  }

  StringRef getPassName() const override { return "Fe Assembly Printer"; }

  virtual bool runOnMachineFunction(MachineFunction &MF) override;

  void emitInstruction(const MachineInstr *MI) override;

  void emitBasicBlockStart(const MachineBasicBlock &MBB) override;
private:
  bool emitPseudoExpansionLowering(MCStreamer &OutStreamer,
                                   const MachineInstr *MI);
  void lowerToMCInst(const MachineInstr *MI, MCInst &Out);
  MCOperand lowerSymbolOperand(const MachineInstr *MI, const MachineOperand &MO) const;

};
}

#endif // LLVM_FEASMPRINTER_H
